﻿
namespace MyFurion.Application.Controller
{
    /// <summary>
    /// 机构
    /// </summary>
    public class OrgController:IDynamicApiController
    {
        private readonly OrgRepository _repository;
        private readonly OrgPerRepository _perRepository;
        public OrgController(OrgRepository userRepository, OrgPerRepository perRepository)
        {
            _repository = userRepository;
            _perRepository = perRepository;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task Add(OrgInfoAdd input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _repository.DataExists(input.OrgCode);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model = input.Adapt<OrgInfo>();
            bool result=await _repository.Add(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task Update(OrgInfoUpdate input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool isExists = await _repository.DataExists(input.OrgCode,input.Id);
            if (isExists)
            {
                throw Oops.Oh(ErrorMsgConst.ExistError);
            }
            var model =await _repository.GetEntityById(input.Id);
            if (model == null)
            {
                throw Oops.Oh(ErrorMsgConst.NoRecord);
            }
            model.ParentId = input.ParentId;
            model.OrgCode = input.OrgCode;
            model.OrgName= input.OrgName;
            model.Status= input.Status;
            bool result = await _repository.Update(model);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.SaveError);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task Delete([FromQuery]BaseIdInput input)
        {
            bool isExists = await _repository.IsExists(it => it.ParentId == input.Id);
            if (isExists)
            {
                throw Oops.Oh("存在下级机构");
            }
            bool result = await _repository.DeleteById(input.Id);
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //机构数据权限数据删除
                await _perRepository.DeleteByWhere(it=>it.OrgId==input.Id);
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("batchDelete")]
        public async Task BatchDelete([FromQuery]BaseIdsInput input)
        {
            bool isExists = await _repository.IsExists(it => input.Ids.Contains(it.ParentId.Value));
            if (isExists)
            {
                throw Oops.Oh("存在下级机构");
            }
            bool result = await _repository.DeleteByWhere(it=>input.Ids.Contains(it.Id));
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.DeleteError);
            }
            else
            {
                //机构数据权限数据删除
                await _perRepository.DeleteByWhere(it =>input.Ids.Contains(it.OrgId));
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("setStatus")]
        public async Task ModifyStatus(SetStatusInfo input)
        {
            if (!Enum.IsDefined(typeof(EnumStatus), input.Status))
            {
                throw Oops.Oh(String.Format(ErrorMsgConst.NotDefined, "状态"));
            }
            bool result = await _repository.Update(it => it.Id == input.Id, it => new OrgInfo() { Status = input.Status });
            if (!result)
            {
                throw Oops.Oh(ErrorMsgConst.StatusError);
            }
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("page")]
        public async Task<PageResult<OrgInfo>>QueryPage(OrgInfoPage input)
        {
            return await _repository.GetPageList(input);
        }
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<List<OrgInfo>> QueryList(OrgInfoPage input)
        {
            return await _repository.GetDataList(input);
        }
        /// <summary>
        /// 根据id获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<OrgInfo> GetDetail(long id)
        {
            return await _repository.GetEntityById(id);
        }
        /// <summary>
        /// 获取全部树数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("tree")]
        public async Task<List<OrgInfo>> QueryTree()
        {
            return await _repository.GetTreeData();
        }
        /// <summary>
        /// 根据条件获取树数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("treeData")]
        public async Task<List<OrgInfo>> QueryTree(OrgInfoPage input)
        {
            return await _repository.GetTree(input);
        }
        /// <summary>
        /// 根据父级id获取下级数据
        /// </summary>
        /// <param name="parentId">父级id</param>
        /// <returns></returns>
        [HttpGet("getByParent")]
        public async Task<List<OrgInfo>> GetByParentId(long parentId)
        {
            return await _repository.GetByParentId(parentId);
        }
    }
}
